package com.uniflow.entity;

import com.baomidou.mybatisplus.annotation.TableName;
import com.fasterxml.jackson.annotation.JsonFormat;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import java.time.LocalDateTime;

/**
 * 系统配置实体类
 * 
 * @author uniflow
 * @since 1.0.0
 */
@ApiModel(description = "系统配置")
@TableName("system_configs")
public class SystemConfig {
    
    @ApiModelProperty("配置ID")
    private String id;
    
    @ApiModelProperty("配置键")
    private String configKey;
    
    @ApiModelProperty("配置值")
    private String configValue;
    
    @ApiModelProperty("配置类型：string/number/boolean/json/encrypted")
    private String configType;
    
    @ApiModelProperty("配置分类")
    private String category;
    
    @ApiModelProperty("配置描述")
    private String description;
    
    @ApiModelProperty("是否公开：1-公开，0-私有")
    private Boolean isPublic;
    
    @ApiModelProperty("是否加密存储")
    private Boolean isEncrypted;
    
    @ApiModelProperty("排序顺序")
    private Integer sortOrder;
    
    @ApiModelProperty("创建人ID")
    private String createdBy;
    
    @ApiModelProperty("更新人ID")
    private String updatedBy;
    
    @ApiModelProperty("创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime createdAt;
    
    @ApiModelProperty("更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
    private LocalDateTime updatedAt;
    
    // 关联查询字段
    @ApiModelProperty("创建人信息")
    private User creator;
    
    @ApiModelProperty("更新人信息")
    private User updater;
    
    // 构造函数
    public SystemConfig() {}
    
    public SystemConfig(String configKey, String configValue, String configType) {
        this.configKey = configKey;
        this.configValue = configValue;
        this.configType = configType;
    }
    
    // Getter和Setter方法
    public String getId() {
        return id;
    }
    
    public void setId(String id) {
        this.id = id;
    }
    
    public String getConfigKey() {
        return configKey;
    }
    
    public void setConfigKey(String configKey) {
        this.configKey = configKey;
    }
    
    public String getConfigValue() {
        return configValue;
    }
    
    public void setConfigValue(String configValue) {
        this.configValue = configValue;
    }
    
    public String getConfigType() {
        return configType;
    }
    
    public void setConfigType(String configType) {
        this.configType = configType;
    }
    
    public String getCategory() {
        return category;
    }
    
    public void setCategory(String category) {
        this.category = category;
    }
    
    public String getDescription() {
        return description;
    }
    
    public void setDescription(String description) {
        this.description = description;
    }
    
    public Boolean getIsPublic() {
        return isPublic;
    }
    
    public void setIsPublic(Boolean isPublic) {
        this.isPublic = isPublic;
    }
    
    public Boolean getIsEncrypted() {
        return isEncrypted;
    }
    
    public void setIsEncrypted(Boolean isEncrypted) {
        this.isEncrypted = isEncrypted;
    }
    
    public Integer getSortOrder() {
        return sortOrder;
    }
    
    public void setSortOrder(Integer sortOrder) {
        this.sortOrder = sortOrder;
    }
    
    public String getCreatedBy() {
        return createdBy;
    }
    
    public void setCreatedBy(String createdBy) {
        this.createdBy = createdBy;
    }
    
    public String getUpdatedBy() {
        return updatedBy;
    }
    
    public void setUpdatedBy(String updatedBy) {
        this.updatedBy = updatedBy;
    }
    
    public LocalDateTime getCreatedAt() {
        return createdAt;
    }
    
    public void setCreatedAt(LocalDateTime createdAt) {
        this.createdAt = createdAt;
    }
    
    public LocalDateTime getUpdatedAt() {
        return updatedAt;
    }
    
    public void setUpdatedAt(LocalDateTime updatedAt) {
        this.updatedAt = updatedAt;
    }
    
    public User getCreator() {
        return creator;
    }
    
    public void setCreator(User creator) {
        this.creator = creator;
    }
    
    public User getUpdater() {
        return updater;
    }
    
    public void setUpdater(User updater) {
        this.updater = updater;
    }
    
    /**
     * 获取配置值并转换为指定类型
     */
    public Object getTypedValue() {
        if (configValue == null) {
            return null;
        }
        
        switch (configType) {
            case "number":
                try {
                    return Long.parseLong(configValue);
                } catch (NumberFormatException e) {
                    try {
                        return Double.parseDouble(configValue);
                    } catch (NumberFormatException ex) {
                        return configValue;
                    }
                }
            case "boolean":
                return Boolean.parseBoolean(configValue);
            case "json":
                // 返回JSON字符串，由调用方解析
                return configValue;
            case "encrypted":
                // 加密值需要解密后返回
                return configValue;
            default:
                return configValue;
        }
    }
    
    /**
     * 设置配置值
     */
    public void setTypedValue(Object value) {
        if (value == null) {
            this.configValue = null;
            return;
        }
        
        this.configValue = value.toString();
    }
    
    @Override
    public String toString() {
        return "SystemConfig{" +
                "id='" + id + '\'' +
                ", configKey='" + configKey + '\'' +
                ", configType='" + configType + '\'' +
                ", category='" + category + '\'' +
                ", isPublic=" + isPublic +
                ", isEncrypted=" + isEncrypted +
                '}';
    }
}